
#ifndef BOARD_H
#define BOARD_H

#include "bsp.h"

#include "common.h"
#include "string_extend.h"
#include "sys_log.h"

//#include "usbd_core.h"
//#include "cdc_class.h"
//#include "cdc_desc.h"
//#include "usbd_int.h"
#include "board_uart.h"
/*----------------bus includes-----------------------------------*/
#include "general_io_bus.h"
#include "pwm_out_bus.h"
#include "lcd_spi_bus.h"
#include "spi_bus.h"
#include "sc8701_bus.h"
#include "key.h"
#include "i2c_simulate_bus.h"
/*----------------device includes-----------------------------------*/
#include "lcd_154HSC04A.h"
#include "buzzer.h"
#include "sc8701.h"
#include "ina226.h"
#include "fusb302_ufp.h"

#define BOARD_KEY_NUM           (5)
#define BOARD_PWM_NUM           (2)
#define BOARD_UART_NUM          (2)

#define BOARD_EEPROM_SIZE       (16*1024ul)
#define BOARD_EEPROM_BASE       (FLASH_BASE + 0x100000 - BOARD_EEPROM_SIZE) //末尾16KB

#define BSP_CDC_USART_RX_BUFF_SIZE      512

enum
{
    BoardSerialDebugID = 0,  
};

enum
{
    kBoardLed_Work = 0,  
    kBoardLed_Alarm,
};

enum
{
    kBoardKey_Left = 0,
    kBoardKey_Right = 1,
    kBoardKey_Enter = 2, 
    kBoardKey_next = 3,
    kBoardKey_PowerEnable = 4, 
};

enum
{
    kBoardPWM_PowerU = 0,  
    kBoardPWM_PowerI,
};

enum
{
    kBoardADC_DN = 0,
    kBoardADC_DP = 1,
    kBoardADC_CPUTemp = 2,
   // kBoardADC_PowerI = 3,
   // kBoardADC_PowerU = 4,
};

typedef struct
{
    TickType_t tick;
    uint8_t enable;
}board_cnt_t;

static inline void board_cnt_sync(board_cnt_t *cnt) {
    cnt->enable = 1;
    cnt->tick = xTaskGetTickCount();
}
static inline uint32_t board_cnt_get_interval(board_cnt_t *cnt) {
    return cnt->enable==0 ? 0 : (xTaskGetTickCount() - cnt->tick)/portTICK_RATE_MS;
}
static void board_cnt_stop(board_cnt_t *cnt) {
    cnt->enable = 0;
}

typedef struct {
    //uint8_t usart_rx_buffer[BSP_CDC_USART_RX_BUFF_SIZE];
    //uint16_t usart_rx_index;
    //uint16_t usart_read_index;
    uint16_t usart_rx_data_len;
    uint8_t usb_buffer[512];
    //linecoding_type linecoding;
    //usbd_core_type usb_core_dev;
}board_cdc_t;


typedef struct
{
    board_uart_handle_t serial[BOARD_UART_NUM];
    /*----------------device-------------------------*/
    led_t lcd_backlight;
    lcd_154hsc04a_t lcd_dev;
    led_t led[2];
    key_t key[BOARD_KEY_NUM];
    sc8701_t sc8701;
    ina226_t ina226_in;
    ina226_t ina226_out;
    FUSB302_dev_t fusb302;
    buzzer_t buzzer;
    
    board_cdc_t cdc;
    int16_t enc_diff; //模拟编码
    uint8_t enc_enter;
}Board;

extern Board board;

extern void board_init(void);
extern void board_get_uid_6(uint8_t *id);
extern int board_serial_send(uint8_t id, uint8_t *buffer, uint32_t length);
extern void BoardTimerTask(uint16_t ms);
extern void board_usb_config(void);
extern void board_pwm_out(uint8_t index, uint16_t duty);
extern uint16_t board_get_adc(uint8_t ch);
extern float board_get_cpu_temp(void);

extern void board_power_enable(uint8_t enable);

extern void board_memory_read(uint32_t read_addr, uint8_t *p_buffer, uint16_t num_read);
extern hal_status_t board_memory_write(uint32_t write_addr, uint8_t *p_buffer, uint16_t num_write);
extern uint8_t board_memory_test(void);
#endif
